<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' :'200px'">
        <!-- 折叠效果 -->
        <div class="toggle-btn" @click="toggleCollapse">||||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          background-color="#606266"
          text-color="#fff"
          active-text-color="#ffd04b" unique-opened="true" :collapse=" isCollapse"  :collapse-transition="false"  router :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu class="icon"
            :index="item.id + ''"
            v-for="item in menuList"
            :key="item.id"
          >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="navState(subItem.path)"
            >
              <!-- 二级菜单的模板 -->
              <template slot="title">
                <i class="el-icon-sunny"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

     <!-- 主体 -->
        <el-main>
            <!-- 这是一个路由点位符 -->
            <router-view></router-view>
        </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单的数据
      menuList: [],
      icons: {
        "125": "iconfont icon-user",
        "103": "iconfont icon-showpassword",
        "101": "iconfont icon-tijikongjian",
        "102": "iconfont icon-baobiao",
        "145": "iconfont icon-morentouxiang"
      },
      //表示不折叠
      isCollapse:false,
      activePath:''
    };
  },
  created() {
    this.getMenuList();
    //从sessionStorage中获取到activaPath并复制给属性activePath
    this.activePath=window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      //清楚sessionStorage中的数据
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      //需要向后台发送axios请求获取数组
      const { data: res } = await this.$http.get("menus");
      console.log(res);
      //判断数据是否获取到了
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
      console.log(res);
    },
    toggleCollapse(){
      //处理菜单的折叠效果
     this.isCollapse=!this.isCollapse
    },
    //链接激活状态
    navState(path){
      window.sessionStorage.setItem("activePath",path)
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  background-color: #303133;
  height: 100%;
}
.el-header {
  background-color: #303113;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 15px;
  }
}
.el-aside {
  background-color: #606266;
  .el-menu{
    border-right: none;
  }
}
.el-main {
  background-color: #dcdfe6;
}
.icon{
  margin: 10px;
}

.toggle-btn{
  border-block-color:#DCDFE6 ;
  font-size: 10px;
  color: #ffffff;
  text-align: center;
  letter-spacing: 2px;
  line-height: 24px;
  cursor: pointer;
  background-color: greenyellow;
}
</style>
